<template>
  <div>
    <!-- 顶部文字及新增按钮 -->

    <div class="wrapper">
      <span>说明：目前支持学科和关键字条件筛选</span>
      <el-button
        type="success"
        icon="el-icon-edit"
        size="small"
        @click="addQuestion"
      >
        新增试题
      </el-button>
    </div>
    <!-- 以上为顶部文字及新增按钮 -->

    <!-- 表单 -->
    <el-form :model="form" class="demo-form-inline" size="small" ref="form">
      <el-row type="flex" class="row-bg">
        <el-col :span="6">
          <el-form-item label-width="80px">
            <template #label>学科</template>
            <el-select
              v-model="form.subjectID"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in simple"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="二级目录" label-width="80px">
            <el-select
              v-model="form.catalogID"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in simpleTwo"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="标签" label-width="80px">
            <el-select
              v-model="form.tags"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in tag"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="关键字" label-width="80px">
            <el-input
              v-model="form.keyword"
              placeholder="根据题干搜索"
              :style="{
                width: '100%'
              }"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" class="row-bg">
        <el-col :span="6">
          <el-form-item label="试题类型" label-width="80px">
            <el-select
              v-model="form.questionType"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in questionType"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="难度" label-width="80px">
            <el-select
              v-model="form.difficulty"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="item in difficulty"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="方向" label-width="80px">
            <el-select
              v-model="form.direction"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item"
                :value="item"
                v-for="item in direction"
                :key="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="录入人" label-width="80px">
            <el-select
              v-model="form.creatorID"
              placeholder="请选择"
              :style="{
                width: '100%'
              }"
            >
              <el-option
                :label="item.username"
                :value="item.id"
                v-for="item in inputMan"
                :key="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row type="flex" class="row-bg">
        <el-col :span="6">
          <el-form-item label="题目备注" label-width="80px">
            <el-input
              v-model="form.remarks"
              :style="{
                width: '100%'
              }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="企业简称" label-width="80px">
            <el-input
              v-model="form.shortName"
              :style="{
                width: '100%'
              }"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="城市" label-width="80px">
            <el-select
              v-model="form.province"
              placeholder="请选择"
              :style="{
                width: '48%',
                marginRight: '2%'
              }"
            >
              <el-option
                :label="item"
                :value="item"
                v-for="item in provinces"
                :key="item"
              ></el-option>
            </el-select>
            <el-select
              v-model="form.city"
              placeholder="请选择"
              :style="{
                width: '50%'
              }"
            >
              <el-option
                :label="item"
                :value="item"
                v-for="item in city"
                :key="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button
              type="primary"
              @click="questionSub"
              :style="{ float: 'right' }"
              >搜索</el-button
            >
            <el-button
              @click="clear"
              :style="{ float: 'right', marginRight: '10px' }"
              >清除</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 以上表单 -->
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects.js'
import { simple as simpleTwo } from '@/api/hmmm/directorys'
import { simple as tag } from '@/api/hmmm/tags'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { simple as users } from '@/api/hmmm/users'
import { provinces, citys } from '@/api/hmmm/citys'
export default {
  name: 'MyForm',
  data () {
    return {
      simple: '',
      simpleTwo: '',
      tag: '',
      questionType: questionType,
      difficulty: difficulty,
      direction: direction,
      inputMan: '',
      provinces: provinces(),
      city: '',
      form: {
        subjectID: '',
        catalogID: '',
        tags: '',
        keyword: '',
        questionType: '',
        difficulty: '',
        direction: '',
        creatorID: '',
        remarks: '',
        shortName: '',
        province: '',
        city: ''
      }
    }
  },

  methods: {
    addQuestion () {
      this.$router.push({ name: 'questions-new' })
    },
    questionSub () {
      this.$emit('search', this.form)
    },
    clear () {
      for (const key in this.form) {
        this.form[key] = ''
      }
    }
  },
  async created () {
    const { data } = await simple()
    const { data: user } = await users()
    this.inputMan = user
    this.simple = data
  },
  watch: {
    async 'form.subjectID' (value) {
      this.form.catalogID = ''
      this.simpleTwo = ''
      this.form.tags = ''
      this.tag = ''
      const { data } = await simpleTwo({ subjectID: value })
      const { data: tags } = await tag({ subjectID: value })
      this.simpleTwo = data
      this.tag = tags
    },
    'form.province' (value) {
      this.form.city = ''
      this.city = ''
      this.city = citys(value)
    }
  }
}
</script>

<style scoped lang="less">
.wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  span {
    font-size: 12px;
    color: pink;
  }
}
</style>
